package com.spzx.cart.controller;

import com.spzx.cart.domain.CartInfo;
import com.spzx.cart.service.ICartService;
import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.common.security.annotation.RequiresLogin;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "购物车接口")
@RestController
@RequestMapping
public class CartController extends BaseController {

	@Autowired
	private ICartService cartService;

	@GetMapping("addToCart/{skuId}/{skuNum}")
	@Operation(summary = "添加商品到购物车")
	@RequiresLogin
	public AjaxResult addToCart(@PathVariable Long skuId,
								@PathVariable Integer skuNum) {
		cartService.addToCart(skuId, skuNum);
		return success();
	}


	// http://localhost:8080/cart/cartList
	// GET
	@GetMapping("cartList")
	@Operation(summary = "获取购物车列表")
	@RequiresLogin
	public AjaxResult cartList() {
		List<CartInfo> cartList = cartService.cartListV2();
		return success(cartList);
	}

	// http://localhost:8080/cart/clearCart
	// GET
	@GetMapping("clearCart")
	@Operation(summary = "清空购物车")
	@RequiresLogin
	public AjaxResult clearCart() {
		cartService.clearCart();
		return success();
	}


	// http://localhost:8080/cart/deleteCart/4
	// DELETE
	@DeleteMapping("deleteCart/{skuId}")
	@Operation(summary = "删除购物车中的商品")
	@RequiresLogin
	public AjaxResult deleteCart(@PathVariable Long skuId) {
		cartService.deleteCartSku(skuId);
		return success();
	}


	// http://localhost:8080/cart/checkCart/4/0
	// GET
	@GetMapping("checkCart/{skuId}/{isChecked}")
	@Operation(summary = "选中/取消选中购物车中的商品")
	@RequiresLogin
	public AjaxResult checkCart(@PathVariable Long skuId,
								@PathVariable Integer isChecked) {
		cartService.checkCart(skuId, isChecked);
		return success();
	}

	// http://localhost:8080/cart/allCheckCart/0
	@GetMapping("allCheckCart/{isChecked}")
	@Operation(summary = "全选/取消全选购物车中的商品")
	@RequiresLogin
	public AjaxResult allCheckCart(@PathVariable Integer isChecked) {
		cartService.allCheckCart(isChecked);
		return success();
	}

	/////////////////////////////////////////////////////
	////////////////远程调用//////////////////////////////
	// 获取购物车选中商品信息
	@GetMapping("getCartCheckedList/{userId}")
	@Operation(summary = "获取购物车选中商品信息")
	@InnerAuth
	public R<List<CartInfo>> getCartCheckedList(@PathVariable("userId") Long userId) {
		List<CartInfo> cartCheckedList = cartService.getCartCheckedList(userId);
		return R.ok(cartCheckedList);
	}

	@GetMapping("deleteCartCheckList")
	@Operation(summary = "删除购物车选中的商品")
	void deleteCartCheckList(@RequestHeader(SecurityConstants.FROM_SOURCE) String source, Long userId){
		cartService.deleteCartCheckList(userId);
	}
}

